<template>
  <view class="container">
    <uni-forms labelWidth="80px" labelPosition="top">
      <!-- <uni-forms-item label="周期" required>
        <uni-data-select v-model="value" :localdata="range" @change="change"></uni-data-select>
      </uni-forms-item>-->
      <uni-forms-item label="周报时间" required>
        <uni-datetime-picker
          @change="changeDay"
          type="daterange"
          return-type="string"
          v-model="times"
        />
      </uni-forms-item>
    </uni-forms>

    <uni-forms labelPosition="top">
      <uni-forms-item label="工作内容">
        <uni-easyinput type="textarea" v-model="msg" placeholder="请输入" autoHeight="true" />
      </uni-forms-item>
    </uni-forms>

    <view style="padding: 20px;"></view>

    <uni-row>
      <button @click="agreeTask()" type="primary">提交</button>
    </uni-row>
  </view>
</template>

<script>
import { websock } from "../../net/websock";
import { addReport } from "../../net/weeklyWork";

export default {
  data() {
    return {
      value: "",
      range: [
        { value: "第一周", text: "第一周" },
        { value: "第二周", text: "第二周" },
        { value: "第三周", text: "第三周" },
        { value: "第四周", text: "第四周" },
        { value: "第五周", text: "第五周" }
      ],
      msg: "",
      users: [],
      times: [], //选中时间
      days: [],
      week: "",
      day: "",
      weeks: [
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
        "星期日"
      ],
      titleName: ""
    };
  },
  async onLoad() {
    // this.getNowDate();
    // this.generateDateModel();
    // this.getWeek();
  },
  methods: {
    async agreeTask() {
      const jxWeeklyReport = {
        timeName: this.titleName,
        content: this.msg
      };
      const res = await addReport(jxWeeklyReport);
      console.log(res);
      if (res.code == 0) {
        let pages = getCurrentPages(); // 当前页面
        const prevPage = pages[pages.length - 2]; //上一页页面实例
        //把数据返回给上一个页面
        console.log(111);
        setTimeout(
          () =>
            uni.navigateBack({
              delta: 1
              //   success: function() {
              //     // prevPage.$vm.getNewestTasksWeiwancheng(); // 执行前一个页面的刷新
              //   }
            }),
          1000
        );
      }
    },
    changeDay(e) {
      console.log(e);
      this.times = e;
      console.log(this.times);
      console.log(this.times[0].substring(5));
      // `${year}年${month}月第${week}周`
      this.titleName = `${this.times[0].substring(
        0,
        4
      )}年(${this.times[0].substring(5)}~${this.times[1].substring(5)})`;
      // this.getWeek(this.times[1]);
    },
    // getWeek(times) {
    //   if (!times) {
    //     let year = new Date().getFullYear();
    //     //月份是从0月开始获取的，所以要+1;
    //     let month1 = new Date().getMonth() + 1;
    //     //日
    //     let day = new Date().getDate();
    //     let time = year + "-" + month1 + "-" + day;
    //     // 将字符串转为标准时间格式
    //     time = Date.parse(time);
    //     time = new Date(time);
    //     // 先计算出该日期为第几周
    //     let week = Math.ceil(time.getDate() / 7);
    //     let month = time.getMonth() + 1;
    //     // 判断这个月前7天是周几，如果不是周一，则计入上个月
    //     if (time.getDate() < 7) {
    //       if (time.getDay() !== 1) {
    //         week = 5;
    //         month = time.getMonth();
    //       }
    //     }
    //     console.log(`${year}年${month}月第${week}周`);
    //     this.titleName = `${year}年${month}月第${week}周`;
    //   } else {
    //     let time = times;
    //     // 将字符串转为标准时间格式
    //     time = Date.parse(time);
    //     time = new Date(time);
    //     // 先计算出该日期为第几周
    //     let week = Math.ceil(time.getDate() / 7);
    //     let month = time.getMonth() + 1;
    //     // 判断这个月前7天是周几，如果不是周一，则计入上个月
    //     if (time.getDate() < 7) {
    //       if (time.getDay() !== 1) {
    //         week = 5;
    //         month = time.getMonth();
    //       }
    //     }
    //     console.log(`${year}年${month}月第${week}周`);
    //     this.titleName = `${year}年${month}月第${week}周`;
    //   }
    // },

    //获取本月天数
    // getNowDate() {
    //   const timeOne = new Date();
    //   const year = timeOne.getFullYear();
    //   let month = timeOne.getMonth() + 1;
    //   this.getMonthDay(year, month);
    // },
    // getMonthDay(year, month) {
    //   let days = new Date(year, month, 0).getDate();
    //   console.log(days);
    //   return days;
    // },
    //获取当前一周
    // generateDateModel() {
    //   const myDate = new Date();
    //   const year = myDate.getFullYear();
    //   const month = myDate.getMonth() + 1;
    //   this.week = myDate.getDay() - 1;
    //   this.day = myDate.getDate();
    //   this.week = this.week === -1 ? this.weeks[6] : this.weeks[this.week];
    //   if (this.week === this.weeks[0]) {
    //     // 如果今天是周一获取本周日期
    //     this.days[0] = `${year}-${month}-${myDate.getDate()}`;
    //     for (let j = 1; j < 7; j += 1) {
    //       const oneDay = new Date(this.days[0]).getTime() + j * 86400000;
    //       this.days[j] = `${new Date(oneDay).getFullYear()}-${new Date(
    //         oneDay
    //       ).getMonth() + 1}-${new Date(oneDay).getDate()}`;
    //     }
    //   } else {
    //     const index = this.weeks.indexOf(this.week); // 找到今天索引
    //     const Monday = Date.now() - index * 86400000;
    //     this.days[0] = `${new Date(Monday).getFullYear()}-${new Date(
    //       Monday
    //     ).getMonth() + 1}-${new Date(Monday).getDate()}`;
    //     for (let i = 1; i < 7; i += 1) {
    //       const oneDay = Monday + i * 86400000;
    //       this.days[i] = `${new Date(oneDay).getFullYear()}-${new Date(
    //         oneDay
    //       ).getMonth() + 1}-${new Date(oneDay).getDate()}`;
    //     }
    //   }
    //   console.log(this.days);
    //   this.times = [this.days[0], this.days[6]];
    // },
    clickSelectMany() {
      uni.navigateTo({
        url:
          "/pages/common/contactSelect?selectedUsers=" +
          JSON.stringify(this.users || []),
        events: {
          contactList: data => {
            console.log("select peitong", data);
            this.users = data.contactList;
          }
        }
      });
    }
  }
};
</script>

<style>
@import url("../common/common.css");
</style>
